//
//  JXLNavigationController.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/9/21.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit

class JXLNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置边缘手势代理
        self.interactivePopGestureRecognizer?.delegate = self
        
       
    }
//  重写该方法监听push的子视图控制及导航视图控制器里面有几个子视图控制器
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //如果个数大于0，表示不是根视图控制器
        if viewControllers.count > 0 {
            //推出第一个控制器，设置左边barButtonItem
            if viewControllers.count == 1 {
                let title = viewControllers.first!.title!
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
                
            } else {
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            }
            
            viewController.navigationItem.title = "当前显示的第\(viewControllers.count + 1)级控制器"
            //隐藏底部tabBar
            viewController.hidesBottomBarWhenPushed = true
            
        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    
    //点击返回barButtonItem
 @objc private func backAction() {
        popViewController(animated: true)
        
    }
    
    //  是否处理这次开始点击的手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //  如果是根视图控制器的话,不处理边缘手势
        if viewControllers.count == 1 {
            return false
        }
        
        
        return true
    }

    
    
    }


